#include "indicators.h"
#include "control.h"
#include "lvgl/lvgl.h"
#include <stdio.h>
#include <stdbool.h>
#include "ui.h"


// 指示灯对象声明
static Indicator ind_left_turn;
static Indicator ind_right_turn; 
static Indicator ind_hazard;
static Indicator ind_qianchegai;
static Indicator ind_houchegai;
static Indicator ind_aqdjs;
static Indicator ind_shache;
static Indicator ind_fadongji;
static Indicator ind_jinguangdeng;
static Indicator ind_yuanguangdeng;
static Indicator ind_aqqn;
static Indicator ind_chemen;
static Indicator ind_qwzd;
static Indicator ind_hwzd;
static Indicator ind_youya;
static Indicator ind_qwd;
static Indicator ind_hwd;
static Indicator ind_shuangshan;
static Indicator ind_abs;
static Indicator ind_eco;

static Indicator ind_wxzsd;
static Indicator ind_zhudong;
static Indicator ind_dlfz;
static Indicator ind_youliangdeng;
static Indicator ind_cldc;
static Indicator ind_cheliangzitai;
static Indicator ind_cheliangxinhao;
static Indicator ind_chaosutixing;
static Indicator ind_yugua;
static Indicator ind_xiansu;
static Indicator ind_aqzsd;
static Indicator ind_chelianwang;
static Indicator ind_zdjs;
static Indicator ind_lqyd;
static Indicator ind_clgy;
static Indicator ind_tyjg;

static Indicator ind_ldxt;
static Indicator ind_ysxt;
static Indicator ind_zny;
static Indicator ind_zqtxh;
static Indicator ind_zhtxh;
static Indicator ind_yqtxh;
static Indicator ind_yhtxh;



// 指示灯状态变量定义
bool left_turn_signal_on = false;
bool right_turn_signal_on = false;
bool hazard_lights_on = false;   
bool qianchegai_on = false;
bool houchegai_on = false;
bool aqdjs_on = false;
bool shache_on = false;
bool fadongji_on = false;
bool jinguangdeng_on = false;
bool yuanguangdeng_on = false;
bool aqqn_on = false;
bool chemen_on = false;
bool qwzd_on = false;
bool hwzd_on = false;
bool youya_on = false;
bool qwd_on = false;
bool hwd_on = false;
bool shuangshan_on = false;
bool abs_active = false;
bool eco_on = false;

bool wxzsd_on = false;
bool zhudong_on = false;
bool dlfz_on = false;
bool youliangdeng_on = false;
bool cldc_on = false;
bool cheliangzitai_on = false;
bool cheliangxinhao_on = false;
bool chaosutixing_on = false;
bool yugua_on = false;
bool xiansu_on = false;
bool aqzsd_on = false; 
bool chelianwang_on = false; 
bool zdjs_on = false;
bool lqyd_on = false;
bool clgy_on = false;
bool tyjg_on = false;

bool ldxt_on = false;
bool ysxt_on = false;
bool zny_on = false;
bool zqtxh_on = false;
bool zhtxh_on = false;
bool yqtxh_on = false;
bool yhtxh_on = false;


bool is_left_signal_animating = false;
bool is_right_signal_animating = false;
    
/**
 * @brief 初始化指示灯模块
 */
void init_indicators() {
    // 运行时初始化指示灯对象
    ind_left_turn = (Indicator){
        .state = false,
        .on_obj = ui_zxdon2,
        .off_obj = ui_zxdoff1
    };
    
    ind_right_turn = (Indicator){
        .state = false,
        .on_obj = ui_yxdon2,
        .off_obj = ui_yxdoff1
    };
    
    ind_wxzsd = (Indicator){
        .state = false,
        .on_obj = ui_jsdon,
        .off_obj = ui_jsdoff
    };
    
    ind_yuanguangdeng = (Indicator){
        .state = false,
        .on_obj = ui_ygdon,
        .off_obj = ui_ygdoff
    };
    
    ind_abs = (Indicator){
        .state = false,
        .on_obj = ui_abson,
        .off_obj = ui_absoff
    };

    ind_qianchegai = (Indicator){
        .state = false,
        .on_obj = ui_qcgon,
        .off_obj = ui_qcgoff
    };

    ind_houchegai = (Indicator){
        .state = false,
        .on_obj = ui_hcgon,
        .off_obj = ui_hcgoff
    };

    ind_aqdjs = (Indicator){
        .state = false,
        .on_obj = ui_aqdjson,
        .off_obj = ui_aqdjsoff
    };

    ind_shache = (Indicator){
        .state = false,
        .on_obj = ui_scon,
        .off_obj = ui_scoff
    };

    ind_fadongji = (Indicator){
        .state = false,
        .on_obj = ui_fdjon,
        .off_obj = ui_fdjoff
    };

    ind_jinguangdeng = (Indicator){
        .state = false,
        .on_obj = ui_jgdon,
        .off_obj = ui_jgdoff
    };

    ind_aqqn = (Indicator){
        .state = false,
        .on_obj = ui_aqqnon,
        .off_obj = ui_aqqnoff
    };

    ind_chemen = (Indicator){
        .state = false,
        .on_obj = ui_chemenon,
        .off_obj = ui_chemenoff
    };

    ind_qwzd = (Indicator){
        .state = false,
        .on_obj = ui_qwzdon,
        .off_obj = ui_qwzdoff
    };

    ind_hwzd = (Indicator){
        .state = false,
        .on_obj = ui_hwzon,
        .off_obj = ui_hwzoff
    };

    ind_youya = (Indicator){
        .state = false,
        .on_obj = ui_yyon,
        .off_obj = ui_yyoff
    };

    ind_qwd = (Indicator){
        .state = false,
        .on_obj = ui_qwdon,
        .off_obj = ui_qwdoff
    };

    ind_hwd = (Indicator){
        .state = false,
        .on_obj = ui_hwdon,
        .off_obj = ui_hwdoff
    };

    ind_shuangshan = (Indicator){
        .state = false,
        .on_obj = ui_sson,
        .off_obj = ui_ssoff
    };
    ind_eco = (Indicator){
        .state = false,
        .on_obj = ui_ecoon,
        .off_obj = ui_ecooff
    };
    ind_zhudong = (Indicator){
        .state = false,
        .on_obj = ui_zdon,
        .off_obj = ui_zdoff
    };
    ind_dlfz = (Indicator){
        .state = false,
        .on_obj = ui_dlfzon,
        .off_obj = ui_dlfzoff
    };
    ind_youliangdeng = (Indicator){
        .state = false,
        .on_obj = ui_yldon,
        .off_obj = ui_yldoff
    };
    ind_cldc = (Indicator){
        .state = false,
        .on_obj = ui_cldcon,
        .off_obj = ui_cldcoff
    };
    ind_cheliangzitai = (Indicator){
        .state = false,
        .on_obj = ui_clzton,
        .off_obj = ui_clztoff
    };
    ind_cheliangxinhao = (Indicator){
        .state = false,
        .on_obj = ui_xhon,
        .off_obj = ui_xhoff
    };
    ind_chaosutixing = (Indicator){
        .state = false,
        .on_obj = ui_cstxon,
        .off_obj = ui_cstxoff
    };
    ind_yugua = (Indicator){
        .state = false,
        .on_obj = ui_yuguaon,
        .off_obj = ui_yuguaoff
    };
    ind_xiansu = (Indicator){
        .state = false,
        .on_obj = ui_xson,
        .off_obj = ui_xsoff
    };
    ind_aqzsd = (Indicator){
        .state = false,
        .on_obj = ui_aqzsdon,
        .off_obj = ui_aqzsdoff
    };
    ind_chelianwang = (Indicator){
        .state = false,
        .on_obj = ui_clwon,
        .off_obj = ui_clwoff
    };
    ind_zdjs = (Indicator){
        .state = false,
        .on_obj = ui_zdjson,
        .off_obj = ui_zdjsoff
    };
    ind_lqyd = (Indicator){
        .state = false,
        .on_obj = ui_lqyon,
        .off_obj = ui_lqyoff
    };
    ind_clgy = (Indicator){
        .state = false,
        .on_obj = ui_clgyon,
        .off_obj = ui_clgyoff
    };
    ind_tyjg = (Indicator){
        .state = false,
        .on_obj = ui_tyjgon,
        .off_obj = ui_tyjgoff
    };
    ind_ldxt = (Indicator){
        .state = false,
        .on_obj = ui_ldxhon,
        .off_obj = ui_ldxhoff
    };
    ind_ysxt = (Indicator){
        .state = false,
        .on_obj = ui_ysxton,
        .off_obj = ui_ysxtoff
    };
    ind_zny = (Indicator){
        .state = false,
        .on_obj = ui_znyon,
        .off_obj = ui_znyoff
    };
    ind_zqtxh = (Indicator){
        .state = false,
        .on_obj = ui_ldxh1on,
        .off_obj = ui_ldxh1off
    };
    ind_zhtxh = (Indicator){
        .state = false,
        .on_obj = ui_ldxh2on,
        .off_obj = ui_ldxh2off
    };
    ind_yqtxh = (Indicator){
        .state = false,
        .on_obj = ui_ldxh3on,
        .off_obj = ui_ldxh3off
    };
    ind_yhtxh = (Indicator){
        .state = false,
        .on_obj = ui_ldxh4on,
        .off_obj = ui_ldxh4off
    };
    
}

/**
 * @brief 更新单个指示灯状态
 * @param ind 指示灯结构体指针
 * @param new_state 新状态
 */
void update_indicator(Indicator *ind, bool new_state) {
    if (new_state != ind->state) {
        ind->state = new_state;
        if (new_state) {
            lv_obj_clear_flag(ind->on_obj, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(ind->off_obj, LV_OBJ_FLAG_HIDDEN);
        } else {
            lv_obj_add_flag(ind->on_obj, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(ind->off_obj, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

/**
 * @brief 更新所有指示灯状态
 */
void update_all_indicators() {
    pthread_mutex_lock(&indicator_mutex);
    // 更新其他指示灯状态
    update_indicator(&ind_qianchegai, qianchegai_on);
    update_indicator(&ind_houchegai, houchegai_on);
    update_indicator(&ind_aqdjs, aqdjs_on);
    update_indicator(&ind_shache, shache_on);
    update_indicator(&ind_fadongji, fadongji_on);
    update_indicator(&ind_jinguangdeng, jinguangdeng_on);
    update_indicator(&ind_yuanguangdeng, yuanguangdeng_on);
    update_indicator(&ind_aqqn, aqqn_on);
    update_indicator(&ind_chemen, chemen_on);
    update_indicator(&ind_qwzd, qwzd_on);
    update_indicator(&ind_hwzd, hwzd_on);
    update_indicator(&ind_youya, youya_on);
    update_indicator(&ind_qwd, qwd_on);
    update_indicator(&ind_hwd, hwd_on);
    update_indicator(&ind_shuangshan, shuangshan_on);
    update_indicator(&ind_abs, abs_active);
    update_indicator(&ind_eco, eco_on);

    update_indicator(&ind_wxzsd, wxzsd_on);
    update_indicator(&ind_zhudong, zhudong_on);
    update_indicator(&ind_dlfz, dlfz_on);
    update_indicator(&ind_youliangdeng, youliangdeng_on);
    update_indicator(&ind_cldc, cldc_on);
    update_indicator(&ind_cheliangzitai, cheliangzitai_on);
    update_indicator(&ind_cheliangxinhao, cheliangxinhao_on);
    update_indicator(&ind_chaosutixing, chaosutixing_on);
    update_indicator(&ind_yugua, yugua_on);
    update_indicator(&ind_xiansu, xiansu_on);
    update_indicator(&ind_aqzsd, aqzsd_on);
    update_indicator(&ind_chelianwang, chelianwang_on);
    update_indicator(&ind_zdjs, zdjs_on);
    update_indicator(&ind_lqyd, lqyd_on);
    update_indicator(&ind_clgy, clgy_on);
    update_indicator(&ind_tyjg, tyjg_on);

    update_indicator(&ind_ldxt, ldxt_on);
    update_indicator(&ind_ysxt, ysxt_on);
    update_indicator(&ind_zny, zny_on);
    update_indicator(&ind_zqtxh, zqtxh_on);
    update_indicator(&ind_zhtxh, zhtxh_on);
    update_indicator(&ind_yqtxh, yqtxh_on);
    update_indicator(&ind_yhtxh, yhtxh_on);

    pthread_mutex_unlock(&indicator_mutex);
}